// pages/addziti/addziti.js
const common = require('../../utils/common.js');
const api = require('../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: -1,
    region: [],
    user_name:'',
    user_tel:'',
    shop_id:'',
    address:'',
    shop_name:''
  },
  bindRegionChange: function (e) {
    let index = e.detail.value;
    let objectArray = this.data.region;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    console.log('id',objectArray[index].id)
    this.setData({
      index: e.detail.value,
      shop_name : objectArray[index].shop_name,
      shop_id : objectArray[index].id,
      address : objectArray[index].shop_addr
    })
  },
  //添加门店
  AddzitiShop() {
    let that = this;
    let data = {
      user_name : that.data.user_name,
      user_tel : that.data.user_tel,
      shop_id : that.data.shop_id||'',
      shop_name : that.data.shop_name||'',
      shop_addr : that.data.address||'',
    }
    common.request(api.AddzitiShop, data, 'post').then((res) => {
      console.log(res)
      if (res.status == 1) {
     
        wx.showToast({
          title: res.msg,
        })
        setTimeout(function(){

          wx.navigateBack({ //返回
            delta: 1
          })
        },1500)
        
      }else{
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }
    })
  },

  get_userShop() {
    let that = this;
    common.request(api.get_userShop, {}, 'post').then((res) => {
      console.log(res)
      if (res.status == 1) {
        that.setData({
          region: res.shoplist,
        })
      }
    })
  },
  //获取我的自提门店
  get_myzitiShop() {
    let that = this;
    let uid = wx.getStorageSync('user_id');
    common.request(api.get_myzitiShop, {
      uid: uid
    }, 'post').then((res) => {
      if (res.status == 1) {
        that.setData({
          shopInfo: res.zitiinfo,
          user_name:res.zitiinfo.user_name,
          user_tel:res.zitiinfo.user_tel,
        })
      }
    })
  },
  tishi(){
   wx.showToast({
     title: '不可修改，如需修改请联系客服',
     icon:'none',
     duration: 2000
   })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      edit:options.edit
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.get_userShop();
    this.get_myzitiShop();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})